package com.lxw.weather.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.lxw.weather.model.DailyForecast;
import com.lxw.weather.R;

import java.util.ArrayList;
import java.util.List;

public class DailyForecastAdapter extends RecyclerView.Adapter<DailyForecastAdapter.ViewHolder> {
    private List<DailyForecast> forecasts = new ArrayList<>();
    private boolean is15DayForecast;  // 标识是否是15天预报

    // 修改构造函数，接收是否15天的标志
    public DailyForecastAdapter(boolean is15DayForecast) {
        this.is15DayForecast = is15DayForecast;
    }

    public void setForecasts(List<DailyForecast> forecasts) {
        this.forecasts = forecasts;
        Log.d("DailyForecastAdapter", "Setting forecasts, count: " + forecasts.size());
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_daily_forecast, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DailyForecast forecast = forecasts.get(position);

        // 通用字段显示
        holder.dateText.setText(forecast.getDate());
        holder.dayTempText.setText("白天: " + forecast.getDayTemp());
        holder.nightTempText.setText("夜间: " + forecast.getNightTemp());
        holder.weatherText.setText(forecast.getDayWeather());
        int iconRes = getWeatherIconRes(forecast.getDayWeatherPic());
        holder.weatherIcon.setImageResource(iconRes);

        // 风向显示（区分7天/15天）
        if (is15DayForecast) {  // 15天预报：合并显示风向
            holder.dayWindText.setText("风向: " + forecast.getDayWindDirection());  // 15天的dayWindDirection已存整体风向
            holder.nightWindText.setVisibility(View.GONE);  // 隐藏夜间风字段
        } else {  // 7天预报：保持原有显示
            holder.dayWindText.setText("白天风: " + forecast.getDayWindDirection());
            if (forecast.getNightWindDirection() != null && !forecast.getNightWindDirection().equals("N/A")) {
                holder.nightWindText.setVisibility(View.VISIBLE);
                holder.nightWindText.setText("/夜间风: " + forecast.getNightWindDirection());
            } else {
                holder.nightWindText.setVisibility(View.GONE);
            }
        }
    }

    private int getWeatherIconRes(String weatherPic) {
        // 根据天气图标名称返回对应资源ID
        if (weatherPic == null || weatherPic.isEmpty()) {
            return R.mipmap.d00; // 默认图标
        }
        
        // 去掉文件扩展名(如果有)
        String iconName = weatherPic;
        if (iconName.contains(".")) {
            iconName = iconName.substring(0, iconName.indexOf('.'));
        }
        
        // 获取资源ID
        try {
            return R.mipmap.class.getField(iconName).getInt(null);
        } catch (Exception e) {
            return R.mipmap.d00; // 默认图标
        }
    }

    @Override
    public int getItemCount() { return forecasts.size(); }
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView dateText, dayTempText, nightTempText, weatherText, dayWindText, nightWindText;
        ImageView weatherIcon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            dateText = itemView.findViewById(R.id.dateText);
            dayTempText = itemView.findViewById(R.id.dayTempText);
            nightTempText = itemView.findViewById(R.id.nightTempText);
            weatherText = itemView.findViewById(R.id.weatherText);
            dayWindText = itemView.findViewById(R.id.dayWindText);
            nightWindText = itemView.findViewById(R.id.nightWindText);
            weatherIcon = itemView.findViewById(R.id.weatherIcon);
        }
    }
}